<?php
/**
 * Created by PhpStorm.
 * DateTime: 2020/11/3 9:06
 * Intro:
 */

namespace addons\podunk\controller;
use think\Exception;
use addons\podunk\library\traits\GoldCache;
use think\Log;

class Device extends Base
{
    use GoldCache;

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        return parent::_initialize();
    }

    public function getList(){
        $search = $this->request->get('search');

        $deviceList = \addons\podunk\model\Device::getDeviceList($search)->toArray();
        foreach ($deviceList['data'] as $key=>&$value){
            if($this->getDeviceStatus($value['identity'].'_'.$this->auth->id.'_'.$value['id'])){
                $value['status'] = 'on';
            }else{
                $value['status'] = 'off';
            }
            $value['task_num'] = $this->getDeviceTaskNum($value['identity'].'_'.$this->auth->id.'_'.$value['id']);
        }
        $deviceList['device_online_num'] = $this->getDeviceOnlineNum($this->auth->id);
        $this->success('设备列表', $deviceList);
    }

    public function add(){
        $params = $this->request->post();
        $this->podunkValidate($params, get_class(), 'add');

        try{
            \addons\podunk\model\Device::create([
                'identity'  => $params['identity'],
                'name' => $params['name'],
                'group_id' => $params['group_id'],
                'user_id' => $this->auth->id
            ]);
            $this->success('添加设备成功');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

    }

    public function edit(){
        $params = $this->request->post();
        $this->podunkValidate($params, get_class(), 'edit');

        try{
            $device = \addons\podunk\model\Device::get($params['id']);
            $device->identity = $params['identity'];
            $device->name = $params['name'];
            $device->group_id = $params['group_id'];
            $device->save();
            $this->success('更新设备成功');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function del(){
        $params = $this->request->post();
        $this->podunkValidate($params, get_class(), 'del');

        try{
            $device = \addons\podunk\model\Device::get($params['id']);
            $device->delete();
            $this->success('删除设备成功');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function getDeviceOnlineList()
    {
        try{
            $deviceIds = '';
            $deviceList = $this->getOnlineDevice($this->auth->id);
            foreach ($deviceList as $device)
            {
                $device = explode('_', $device);
                if(count($device) == 3){
                    if($deviceIds){
                        $deviceIds = $deviceIds.','.$device[2];
                    }else{
                        $deviceIds = $device[2];
                    }

                }
            }
            $map['id'] = ['in',$deviceIds];
            $onlineDevice = \addons\podunk\model\Device::where($map)->paginate(10);
        }catch (Exception $e){
            Log::error($e->getMessage());
            $this->error($e->getMessage());
        }


        $this->success('获取在线设备列表成功',$onlineDevice);
    }
}